package com.norns.torque.dialog

import android.animation.ValueAnimator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import com.hi.dhl.binding.viewbind
import com.norns.torque.databinding.DialogLoadingBinding
import com.norns.torque.utils.AppContext
import com.norns.torque.utils.DisplayUtil

class LoadingDialog :CommonDialog() {
    private val binding by viewbind<DialogLoadingBinding>()
    private var mValueAnimator:ValueAnimator?=null
    companion object {
        fun newInstance(content:String): LoadingDialog {
            val args = Bundle()
            val fragment = LoadingDialog()
            args.putString("content",content)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        dialogWidth= DisplayUtil.dip2px(AppContext.context,100f)
        dialogHeight= DisplayUtil.dip2px(AppContext.context,80f)
        isCancelableOutside=false
        return binding.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val backgroundRes=arguments?.getInt("background")?:0
        val content=arguments?.getString("content")?:""
        if (backgroundRes!=0){
            binding.root.setBackgroundResource(backgroundRes)
        }
        if (content.isNotEmpty()){
            binding.tvText.text=content
        }
        mValueAnimator = ValueAnimator.ofInt(30, 3600).apply {
            setDuration(3000)
            setInterpolator(LinearInterpolator())
            setRepeatCount(ValueAnimator.INFINITE)// 动画无限循环执行
            setRepeatMode(ValueAnimator.RESTART)// 每次重复从头开始
            start()
            addUpdateListener { animation->
                val value = animation.animatedValue as Int
                binding.loading.rotation=30f * (value / 30f)
            }
        }
    }

    override fun onPause() {
        super.onPause()
        mValueAnimator?.cancel()
    }


}